package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.util.LoginAuthUtil;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.MenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//描述当前Controller
@Tag(name="首页模块" ,description = "首页：登录注销获取验证码")
@RequestMapping("/admin/system/index")
// allowCredentials：允许浏览器异步携带cookie访问   originPatterns:* 允许所有的服务器跨域访问
//跨域配置的值只能有一个  不能有多个
//@CrossOrigin(allowCredentials = "true" , originPatterns = "*",
//        allowedHeaders="*" )
public class IndexController {
    @Resource
    SysUserService sysUserService;
    @Resource
    SysMenuService sysMenuService;
    @Resource
    ValidateCodeService validateCodeService;
    @Operation(summary = "查询登录用户分配的菜单列表")
    @GetMapping("menus")
    public Result<List<MenuVo>> listMenus(){
        List<MenuVo> menuVos = sysMenuService.listMenusByLoginUserId();
        return Result.ok(menuVos);
    }

    //4、注销
    // http://localhost:8501/admin/system/index/logout
    // 当访问的路径没有匹配到controller接口处理时，使用默认的错误Controller处理
    //描述一个接口
    @Operation(description="注销",summary = "注销")
    @Parameters(value = {
            @Parameter(name = "令牌" , required = true , allowEmptyValue = false,
            in = ParameterIn.HEADER)
    })
    @PostMapping("logout")
    public Result<Object> logout(@RequestHeader(value = "token") String token){
        sysUserService.logout(token);
        return Result.build(ResultCodeEnum.SUCCESS , null);
    }
    //3、根据token查询用户信息
    // 通过请求头传参：  token  token的值
    @Operation(description="获取用户信息" , summary = "获取用户信息")
    @Parameters(value = {
            @Parameter(name = "令牌" , required = true , allowEmptyValue = false,
                    in = ParameterIn.HEADER)
    })
    @GetMapping("/userInfo")
    public Result<SysUser> userInfo(@RequestHeader("token") String token){
//        SysUser sysUser = sysUserService.getUserInfo(token);
//        System.out.println(LoginAuthInterceptor.CACHE.get(Thread.currentThread()));
//        System.out.println(LoginAuthInterceptor.THREAD_LOCAL.get());

        return Result.build(ResultCodeEnum.SUCCESS , LoginAuthUtil.get());
    }
    //2、获取图片验证码
    @Operation(description="获取图片验证码", summary = "获取图片验证码")
    @GetMapping("/genValidateCode")
    public Result<ValidateCodeVo> genValidateCode(){
        ValidateCodeVo codeVo = validateCodeService.genValidateCode();
        return Result.build(ResultCodeEnum.SUCCESS,codeVo);
    }
    //1、登录
    @Operation(description="登录", summary = "登录")
    @PostMapping("login")
//    public Result login(LoginDto loginDto){  /admin/system/index/login?userName=zhangsan&password=122314234
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){//接收请求体参数
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(ResultCodeEnum.SUCCESS , loginVo);
    }
}
